#include "DE15.h"
#include "main.h"
#include "stm32f1xx_hal.h"
#include "dataprocess.h"
/*UART Receive Buffer Size*/
struct s_filter lpFilter;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	
	uint8_t i=0;
	uint8_t *p_temp;
	static int index=0;
	int32_t adc_datax = 0;
	uint8_t dummy[27]={0xff};
	static int32_t counter=0;
	if(GPIO_Pin == GPIO_PIN_8)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);		
		HAL_SPI_TransmitReceive_DMA(&hspi1,dummy,g_u8_spi_rddata,27);
		while(HAL_SPI_GetState(&hspi1)!=HAL_SPI_STATE_READY);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		temp=(g_u8_spi_rddata[0]<<12);
		tempH=(g_u8_spi_rddata[1]<<4);
		tempL=(g_u8_spi_rddata[2]>>4);
		t=temp+tempH+tempL;
		for(int i=0;i<16;i++)
		{
			if(is_bit_set(t,i))
				status_PN[i]++;
			else
				status_PN[i]=0;	
		}
		p_temp = g_u8_spi_rddata + 3;
		for(uint8_t i = 0; i < CHANNEL; i++)
		{
			adc_datax = (uint32_t)(p_temp[0]<<16) + (uint32_t)(p_temp[1]<<8)+ (uint32_t)p_temp[2];
			adc_datax <<= 8;
			adc_datax >>= 8;
			g_i32_adcdata_FIFO[g_i16_adcdata_FIFO_end][i] = adc_datax/6;
			p_temp += 3;
		}
	
		//	printf("%d\r\n",g_i32_adcdata_FIFO[g_i16_adcdata_FIFO_end][0]);
		g_i16_adcdata_FIFO_end++;
		if(ADCDATA_FIFO_DEPTH == g_i16_adcdata_FIFO_end)
		{				
			g_i16_adcdata_FIFO_end = 0;	
		}
		counter++;
		if(counter==1000)
		{
			counter=0;
			gflag_uploadstatus=1;
			
		}
		//HAL_GPIO_TogglePin(GPIOB,MCU_wdi_Pin);
  } 

}

void lpFilterInit(int channle)
{
	lpFilter.m_chmove=12;
	lpFilter.m_nCoefNumb=2;
	lpFilter.m_pa[0]=4096;
	lpFilter.m_pa[1]=-3440;
	lpFilter.m_pb[0]=328;
	lpFilter.m_pb[1]=328;
	for(int8_t i=0;i<channle;i++)
	{
		lpFilter.m_px[channle][0]=0;
		lpFilter.m_px[channle][1]=0;
		lpFilter.m_py[channle][0]=0;
		lpFilter.m_py[channle][1]=0;
	}

}
int dataLpFilter(int xt,int index)
{

        int i=0;
        I64 tmp=0;
        I64 yt=0;
        for(i=2-1;i>0;i--)
        {
                lpFilter.m_px[index][i]=lpFilter.m_px[index][i-1];
                lpFilter.m_py[index][i]=lpFilter.m_py[index][i-1];
        }
        lpFilter.m_px[index][0]=(I64)xt;

        for(i=0,tmp=0;i<lpFilter.m_nCoefNumb;i++)
        {
                tmp+=lpFilter.m_px[index][i]*lpFilter.m_pb[i];
                if(i)
                tmp-=lpFilter.m_py[index][i]*lpFilter.m_pa[i];
        }

        yt=tmp>>lpFilter.m_chmove;
        lpFilter.m_py[index][0]=yt;

        return yt;
////    /*for debug*/
////    return xt;
}


